package org.jsola.contract.service.impl;

import org.jsola.contract.common.ExampleKit;
import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dao.IContractPayLogDAO;
import org.jsola.contract.dto.ContractPayLogAddDTO;
import org.jsola.contract.dto.ContractPayLogUpdateDTO;
import org.jsola.contract.entity.ContractPayLogDO;
import org.jsola.contract.query.ContractPayLogQuery;
import org.jsola.contract.service.IContractPayLogService;
import org.jsola.contract.vo.ContractPayLogListVO;
import org.jsola.contract.vo.ContractPayLogVO;
import org.jsola.core.Page;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 租户电子合同充值记录
 *
 * @author JanusMix
 */
@Service("contractContractPayLogServiceImpl")
public class ContractPayLogServiceImpl implements IContractPayLogService {

    @Autowired
    private IContractPayLogDAO contractPayLogDAO;

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public ContractPayLogVO save(ContractPayLogAddDTO contractPayLogAddDTO, TokenUser tokenUser) {
        // 转DO
        ContractPayLogDO contractPayLogDO = contractPayLogAddDTO.to(ContractPayLogDO.class);
        // 保存
        contractPayLogDO = save(contractPayLogDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return contractPayLogDO.to(ContractPayLogVO.class);
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int update(ContractPayLogUpdateDTO contractPayLogUpdateDTO, TokenUser tokenUser) {
        //转DO
        ContractPayLogDO contractPayLogDO = contractPayLogUpdateDTO.to(ContractPayLogDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(contractPayLogDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = ContractConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... contractPayLogIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) contractPayLogIds);
    }


    @Override
    public ContractPayLogVO selectById(Long contractPayLogId, String siteId) {
        ContractPayLogDO contractPayLogDO = selectDOById(contractPayLogId, siteId);
        if (contractPayLogDO == null) {
            return null;
        }
        return contractPayLogDO.to(ContractPayLogVO.class);
    }

    @Override
    public List<ContractPayLogListVO> select(ContractPayLogQuery contractPayLogQuery, String siteId) {
        List<ContractPayLogDO> contractPayLogDOList = selectDO(contractPayLogQuery, siteId);
        if(CollectionUtils.isEmpty(contractPayLogDOList)) {
            return contractPayLogDOList == null ? null : new ArrayList<>();
        }
        return contractPayLogDOList.stream()
                .map(contractPayLogDO -> contractPayLogDO.to(ContractPayLogListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ContractPayLogQuery contractPayLogQuery, String siteId) {
        Example example = buildExample(contractPayLogQuery,siteId);
        return contractPayLogDAO.selectCountByExample(example);
    }

    @Override
    public Page<ContractPayLogListVO> selectPage(ContractPayLogQuery contractPayLogQuery, String siteId) {
        Example example = buildExample(contractPayLogQuery,siteId);
        Page<ContractPayLogDO> page = contractPayLogDAO.selectPageByExample(example,
                contractPayLogQuery.getPageNo(),
                contractPayLogQuery.getPageSize());

        return page.to(ContractPayLogListVO.class);
    }

    @Override
    public ContractPayLogDO selectDOById(Long contractPayLogId, String siteId) {
        return listById(contractPayLogId, siteId);
    }

    @Override
    public List<ContractPayLogDO> selectDO(ContractPayLogQuery contractPayLogQuery, String siteId) {
        Example example = buildExample(contractPayLogQuery,siteId);
        return contractPayLogDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example

     * @param contractPayLogQuery 查询参数
     * @param siteId 所属站点id
     * @return example
     */
    private Example buildExample(ContractPayLogQuery contractPayLogQuery, String siteId) {
        Example example = new Example(ContractPayLogDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example,contractPayLogQuery.getOrders());
        return example;
    }
}




